/*********************************************************************************
Description: 
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11 

Copyright (c) 2012-2022 School of Software Engineering of USTC, All rights reserved.

**********************************************************************************/



#include "client.h"




/*****************************************************************
Function Name: main
Description:
Parameter:  

@argc: argument count
@argv: argument vector

Author: 
Date:  
******************************************************************/
int main(int argc, char** argv)
{
    int sockfd;
    char recvline[MAXLINE], sendline[MAXLINE];
    struct sockaddr_in servaddr;

    /* check whether argument count is right */
    if(argc != 2)
    {
        printf("usage: ./client <ipaddress>\n");
        
        return 0;
    }
       
    /* generate a new socket for client */
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
        
        return 0;
    }
        
    /* initialize structure sockaddr_in with protocol family, port number, ip */
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(10000);
            
    if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    {
        printf("inet_pton error for %s\n", argv[1]);
        
        return 0;
    }
        
    /* connect server using socket id */
    if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
    {
        printf("connect error: %s(errno: %d)\n",strerror(errno), errno);
        
        return 0;
    }

    /* send messages to server */
    printf("Please type messages: \n");
    fgets(sendline, MAXLINE, stdin);
    if(send(sockfd, sendline, strlen(sendline), 0) < 0)
    {
        printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
        
        return 0 ;
    }

    close(sockfd);
    return 0;
}
